import moment from 'moment'

export class DateHelper {
  constructor () {
    this.today = moment().startOf('day')
    this.lastMonth = moment(new Date().getTime()).add(1, 'month').startOf('day')
    const day = this.lastMonth.day()
    this.lastMonthSunday = this.lastMonth.clone()
    if (day === 0) {
      this.lastMonthSunday.startOf('day')
    } else {
      this.lastMonthSunday.add(7 - day, 'day').startOf('day')
    }
    console.log(
      this.today.format('YYYY-MM-DD'),
      this.lastMonth.format('YYYY-MM-DD'),
      this.lastMonthSunday.format('YYYY-MM-DD')
    )
    this.calcDateList()
  }

  /// 计算排产日期
  calcDateList () {
    var dateList = []
    var date = this.today.clone()
    while (date.valueOf() < this.lastMonthSunday.valueOf()) {
      dateList.push({
        title: date.format('MM-DD'),
        value: date.valueOf(),
        date: date.format('YYYY-MM-DD'),
        isDay: true
      })
      date.add(1, 'day')
    }
    /// 一个月时长
    this.daysCount = dateList.length
    /// 周数显示
    this.weeksCount = 6
    var friday = this.lastMonthSunday.clone().add(5, 'day')
    // var weekCount = parseInt(this.lastMonthSunday.diff(this.today, 'day') / 7) + 1
    for (var i = 0; i < this.weeksCount; i++) {
      dateList.push({
        value: friday.valueOf(),
        title: friday.format('MM-DD'),
        // title: '第' + (weekCount + i) + '周',
        date: friday.format('YYYY-MM-DD'),
        isWeek: true
      })
      friday.add(1, 'week')
    }
    this.dateList = dateList
  }
}
